#coding=utf8
import datetime
import urllib2,re
import PyRSS2Gen


'''
Created on 2010-5-20

@author: Fred <me@fengsage.cn>
'''


REG_BLOG_PAGE = u'<a href=([/,"]?/[^>]*/blog/index/\d*["]?)>'
REG_BLOG_TITLE = u'<div class="tit"><a href="(/[^>]*/blog/item/[^>]*.html)" target="_blank">'
REG_ARTICLE_TITLE = u'<div class="tit">([^>,/,<]*)</div>'
REG_ARTICLE_CONTENT = u'<td><div id="blog_text" class="[^>]*" [\s\S]*?</div></td>'
REG_ARTICLE_CATAGORY = u'<a href="/[^>]*/blog/category/[^>]*>([^>]*)</a>'
REG_ARTICLE_DATETIME = u'<div class="date">([^>,/,<]*)</div>'


class BaiduMove(object):

    BAIDU_HOME = 'http://hi.baidu.com/%s'
    
    author = 'Fred'
    blog_id = ''
    
    def __init__(self,author,blog_id):
        self.author = author
        self.blog_id = blog_id
        
    def readPage(self,url):
        #模拟浏览器
        header = { 'Host':'hi.baidu.com',\
                   'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9',\
                   'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',\
                   'Accept-Language':'zh-cn,zh;q=0.5'}
        
        req = urllib2.Request(url,None,header)
        
        try:
            response = urllib2.urlopen(req)
        except:
            print 'open exception'
            return 0
        html_content = response.read().replace('&nbsp;','').decode('gbk') #百度空间需要gbk来解码
        return html_content
    
    #根据表达式返回列表
    def findList(self,content,reg):
        link_list = re.findall(reg,content)
        return link_list
    
    #页面采集
    def articleList(self):
        article_list = []
        blog_content = self.readPage(self.BAIDU_HOME%(self.blog_id+'/blog'))
        blog_page_url = self.findList(blog_content,REG_BLOG_PAGE)
        if blog_page_url==[]:
            print '您的博客ID输入错误!或该博客文章数过少'
            return 0

        min_page = int(blog_page_url[0].replace('"','').replace('/%s/blog/index/'%self.blog_id,'')) #第一页
        max_page = int(blog_page_url[-1].replace('"','').replace('/%s/blog/index/'%self.blog_id,'')) #最大页
        
        if min_page == max_page-1:
            max_page = int(blog_page_url[-2].replace('"','').replace('/%s/blog/index/'%self.blog_id,'')) #最大页
        
        print '博客%s共%s页'%(self.blog_id,max_page+1)
        
        for page_index in range(0,max_page+1):
            page_url = self.BAIDU_HOME%(self.blog_id+'/blog/index/'+str(page_index))
            print '第%d页%s分析...'%(page_index+1,page_url)
            page_content = self.readPage(page_url)
            article_url = self.findList(page_content, REG_BLOG_TITLE)
            for article in article_url:
                article_url = self.BAIDU_HOME%(article[1:])
                article_list.append(article_url)
        print '共发现%s篇博文'%len(article_list)
        return  article_list

    #文章采集
    def articleBody(self,url):
        print '读取%s'%url
        article_html = self.readPage(url).replace('\n','')
#        print article_html
        #获取博文标题
        article_title = self.findList(article_html, REG_ARTICLE_TITLE)[0]        
        article_title = re.sub('&#(\d+);', '-', article_title)
#        ''.join([unichr(int(x)) for x in re.findall(r'&#(\d+);', article_title)])
        
        #获取博文时间
        article_date = self.findList(article_html, REG_ARTICLE_DATETIME)[0]
        #获取博文分类
        article_category = self.findList(article_html, REG_ARTICLE_CATAGORY)[0].replace(u'类别：','')
        #获取博文正文
        article_content = self.findList(article_html, REG_ARTICLE_CONTENT)[0]\
                                .replace('id="blog_text" class="cnt"','')[12:][:-11]
#        print article_content
        article = {'article_url':url,\
                   'article_title':article_title,\
                   'article_date':article_date,\
                   'article_category':article_category,\
                   'article_content':article_content,\
                   'article_author':self.author}
        return article
    
    def rss_item(self,articleList):
        rss_item = []
        article_url_list = articleList
        for article_url in article_url_list:
            acticle_body = self.articleBody(article_url)
            rss_item.append(
                    PyRSS2Gen.RSSItem(
                        title = self.formatString(acticle_body.get('article_title')),
                        link = self.formatString(acticle_body.get('article_url')),
                        description = self.formatString(acticle_body.get('article_content')),
                        categories = [acticle_body.get('article_category')],
                        guid = PyRSS2Gen.Guid(acticle_body.get('article_url')),
                        pubDate = acticle_body.get('article_date'),
                        author = self.formatString(acticle_body.get('article_author'))))
        return rss_item
    
    #输出RSS
    def articleToRSS(self,filename,cut=1):
        articleList = self.articleList()
        if cut>1:
            print '生成%d份文件'%cut

        #计算分割范围,140/5=28,184/5=36
        articleCount = len(articleList)
        articlePage = articleCount/cut
        #切割文件
        for i in range(0,cut):
            print '分割范围%d-%d'%(articlePage*i,articlePage*(i+1))
            if i != (cut-1):
                rss_item = self.rss_item(articleList[articlePage*i:articlePage*(i+1)])
            else:
                rss_item = self.rss_item(articleList[articlePage*i:articleCount])
            rss = PyRSS2Gen.RSS2(
                title = "百度空间搬家工具",
                link = "http://fengsageblog.appspot.com",
                description = "百度空间搬家工具",
                copyright = '百度空间搬家工具 ad@fengsage.cn',
                lastBuildDate = datetime.datetime.now(),
                items = rss_item               
            )
            rss.write_xml(open(filename+"_%d.xml"%i, "w"),'utf-8')
        print '搬家结束'
    
    
    #字符转义
    def formatString(self,str):
        format_str = '<![CDATA['+str+']]>'
        return format_str
    
if __name__=='__main__':
    baidu = BaiduMove('fred','429263181')
    baidu.articleToRSS('Hibaidu_fred',10)
#    print baidu.articleBody('http://hi.baidu.com/429263181/blog/item/ffc01946d29129016b63e5c6.html')
    
    
